package com.ruoyi.analytics.controller;

import com.ruoyi.analytics.domain.AnalyticsEvent;
import com.ruoyi.analytics.service.IAnalyticsEventService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/analytics/event")
public class AnalyticsEventController extends BaseController {

    @Autowired
    private IAnalyticsEventService analyticsEventService;

    /**
     * 埋点数据上报接口
     */
    @PostMapping
    public AjaxResult add(@RequestBody AnalyticsEvent event) {
        // @RequestBody 会自动将JSON转为AnalyticsEvent对象
        // 做一些基础校验
        if (event == null || StringUtils.isEmpty(event.getEventName())) {
            return AjaxResult.error("Event data or event name is empty.");
        }
        analyticsEventService.saveEvent(event);
        // 因为是异步的，所以立即返回成功
        return AjaxResult.success("Event received.");
    }

    /**
     * 查询埋点数据列表
     * 前端会通过查询参数传递过来
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        // startPage(); // MongoDB分页不需要这个
        TableDataInfo data = analyticsEventService.queryEventList(params);
        return data;
    }
}
